package com.project.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.project.bean.CoachBean;
import com.project.bean.CommentBean;
import com.project.bean.ContractBean;
import com.project.bean.FolloweBean;
import com.project.bean.GymBean;
import com.project.bean.GymDTO;
import com.project.bean.PageBean;
import com.project.bean.PostBean;
import com.project.bean.UserBean;
import com.project.service.ICoachService;
import com.project.service.ICommentService;
import com.project.service.IContractService;
import com.project.service.IFollowService;
import com.project.service.IGymService;
import com.project.service.ILessonService;
import com.project.service.IPostService;
import com.project.service.IStudentService;
import com.project.service.IUserService;
import com.project.util.UploadImgUtil;


@Controller
public class CoachController {
	@Autowired
	private IPostService postService;
	@Autowired
	private IContractService contractService;
	@Autowired
	private ILessonService lessonService;
	@Autowired
	private ICoachService coachService;	
	@Autowired
	private IUserService userservice;	
	@Autowired
	private ICommentService commentService;
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IFollowService followService;
	@Autowired
	private IGymService gymservice;
	
	//访问教练
	@RequestMapping("/coach_idex_other")
	public String coachIndexOther(Model model,int cUId) {
		PageBean posts=postService.findUserPostsByPage(cUId, 1, 5);
		PageBean contracts = contractService.findContractByStateAndPage(4, cUId, 5, 1);
		CoachBean coach = coachService.findCoachById(cUId);
		model.addAttribute("posts", posts);
		model.addAttribute("contracts", contracts);
		model.addAttribute("coach", coach);
		return "coach_idex_other.html";
	}
	//添加关注
	@RequestMapping("/followCoach")
	@ResponseBody
	public int addFollow(HttpServletRequest req,int cUId) {
		Integer uid=(Integer) req.getSession().getAttribute("user");				
		//判断是否已经关注
		if (followService.findFollow(uid, cUId)>0) {
			return 1;
		}else {
			if (followService.addFollow(uid, cUId)>0) {
				return 2;
			}
		}		
		return 0; 

	}
	
	
	@RequestMapping(value="/coach_idex_other_refresh",method=RequestMethod.POST)
	public String coachIndexOtherPost(Model model,int cUId,int page) {
		PageBean posts=postService.findUserPostsByPage(cUId, page, 5);
		model.addAttribute("posts", posts);
		return "coach_idex_other::post_refresh";
	}
	
	
	
	@RequestMapping("/coach_idex_self")
	public String coachIndexSelf(Model model,int cUId) {
		PageBean posts=postService.findUserPostsByPage(cUId, 1, 5);
		PageBean contracts = contractService.findContractByStateAndPage(4, cUId, 5, 1) ;
		CoachBean coach = coachService.findCoachById(cUId);
		model.addAttribute("posts", posts);
		model.addAttribute("contracts", contracts);
		model.addAttribute("coach", coach);
		return "coach_idex_other.html";		
	}
	
		//登录成功,进入主页
		@GetMapping("/coach/{uid}")
		public String showCoach(@PathVariable("uid")Integer uId,HttpServletRequest req) {	 
			CoachBean coach=coachService.findCoachById(uId);			
			//将当前用户的id存入session
			 HttpSession session = req.getSession();	
			 session.setAttribute("user", uId);		
			return "redirect:/coachPosts?page=1&size=5";	
		}
		
		//动态显示我的主页
		@GetMapping("/coachPosts")
		public String showMyPosts(HttpServletRequest req,ModelMap map,int page,int size) {
			Integer uid=(Integer) req.getSession().getAttribute("user");
			//分页显示我的动态
			PageBean pageBean=postService.findUserPostsByPage(uid, page, size);
			//找到推荐场馆
			List<UserBean> list=userservice.findUsersByType(3);				
			map.addAttribute("Gyms", list);
			map.addAttribute("pageBean", pageBean);
			return "/coach_idex_self.html";
			
		}
		
		
		//进入完善(修改)信息页面
		@GetMapping("/coachInfo")
		public String coachMessage(HttpServletRequest req,ModelMap model){

			return "/coach_info_change.html"; 		
		}
	
		//进入查看附近学员
		@GetMapping("/coachFindStudents")
		public String showAllStudents(HttpServletRequest req,ModelMap map,int page,int size) {	 
			PageBean stuPage=studentService.findAllStudentToMap(page, size);
			map.addAttribute("stuPage", stuPage);
			System.out.println(stuPage);
			return "/gymmapteststudent.html";	
		}
		
		//返回教练主页
		@GetMapping("/coach")
		public String showCoach(HttpServletRequest req) {	 
			//获取当前用户的id
			Integer uid=(Integer) req.getSession().getAttribute("user");
			//System.out.println(uid);
			return "redirect:/coachPosts?page=1&size=4";	
		}
	
		//显示好友的动态
		@GetMapping("/coachFriendsPosts")
		@ResponseBody
		public PageBean showfriendsPosts(HttpServletRequest req,ModelMap map,int page,int size) {
			Integer uid=(Integer) req.getSession().getAttribute("user");
			PageBean pageBean=postService.findAllFollowPosts(uid, page, size);
			
			return pageBean;			
		}
		//查看粉丝
		@GetMapping("/coachFans")
		@ResponseBody
		public List<FolloweBean> findFansById(HttpServletRequest req){
			Integer uid=(Integer) req.getSession().getAttribute("user");
			List<FolloweBean> list=followService.findFansById(uid);
			return list;		
		}
		//查看关注
		@GetMapping("/coachFollow")
		@ResponseBody
		public List<FolloweBean> findFollowsById(HttpServletRequest req){
			Integer uid=(Integer) req.getSession().getAttribute("user");
			
			List<FolloweBean> list=followService.findFollowsById(uid);
			return list;	
		}		

		//发布动态
		@PostMapping("/coachPost")
		public String addPost(String Message,HttpServletRequest req,MultipartFile imgName,ModelMap map) {	
			Integer uid=(Integer) req.getSession().getAttribute("user");
			PostBean post=new PostBean();
			String pictureName=UploadImgUtil.uploadimg(req, imgName); 
			post.setpImg(pictureName);
			post.setpUId(uid);
			post.setpMessage(Message);
	        Date date=new Date();
			post.setpTime(date);
			System.out.println(post);
			postService.addPost(post);		
		    return "redirect:/coachPosts?page=1&size=4";
		
		}
		
		//发送评论
		@PostMapping("/coachPostComment")
		@ResponseBody
		public CommentBean addComment(String Message,HttpServletRequest req,MultipartFile imgName,ModelMap map) {	
			CommentBean comment = new CommentBean();
			comment.setCmMessage(Message);
			comment.setCmPId(3);
			comment.setCmUId(3);		
	        Date date=new Date();
			comment.setCmTime(date);
			System.out.println(comment);
			commentService.addComment(comment);		
		    return comment;
		
		}
		//显示当前动态的评论
		@GetMapping("/coachPostComments")
		public String showComments(ModelMap map) {
			List<CommentBean> list=commentService.findCommentsByPost(43);		
			map.addAttribute("list", list);
			for (CommentBean commentBean : list) {
				System.out.println(commentBean);
			}
			return "/coach_idex_self.html";		
		}	
			
			
			
			
	
	
}
